/**
 * version 1.1
 */
function dateFormat(fmt, date) {
	let ret;
	const opt = {
		"Y+": date.getFullYear().toString(), // 年
		"m+": (date.getMonth() + 1).toString(), // 月
		"d+": date.getDate().toString(), // 日
		"H+": date.getHours().toString(), // 时
		"M+": date.getMinutes().toString(), // 分
		"S+": date.getSeconds().toString() // 秒
		// 有其他格式化字符需求可以继续添加，必须转化成字符串
	};
	for (let k in opt) {
		ret = new RegExp("(" + k + ")").exec(fmt);
		if (ret) {
			fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
		};
	};
	return fmt;
}
/**
 * @param {Object} time 时间戳 如果时间戳为10位则转换为13位
 */
function getNowFormatDate(time) {
	if (time.length == 10) {
		time *= 1000;
	}
	var date = new Date(time);
	var seperator1 = '-';
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = '0' + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = '0' + strDate;
	}
	var currentdate = year + seperator1 + month + seperator1 + strDate;
	return currentdate;
}

function getSevenAfterDate() {
	var date = new Date();
	var seperator1 = '-';
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var strDate = date.getDate() - 7;
	if (month >= 1 && month <= 9) {
		month = '0' + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = '0' + strDate;
	}
	var currentdate = year + seperator1 + month + seperator1 + strDate;
	return currentdate;
}
/**
 * 获取今天时间
 * type 获取时间类型 1 字符串   2数组
 * 默认为1
 */
function getTodayDate(type = 1) {
	var date = new Date();
	var seperator1 = '-';
	var seperator2 = ':';
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	var hhstr = date.getHours();
	var mmStr = date.getMinutes();
	var ssStr = date.getSeconds();
	if (month >= 1 && month <= 9) {
		month = '0' + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = '0' + strDate;
	}
	if (hhstr >= 0 && hhstr <= 9) {
		hhstr = '0' + hhstr;
	}
	if (mmStr >= 0 && mmStr <= 9) {
		mmStr = '0' + mmStr;
	}
	if (ssStr >= 0 && ssStr <= 9) {
		ssStr = '0' + ssStr;
	}
	return (year + seperator1 + month + seperator1 + strDate + ' ' + hhstr + seperator2 + mmStr + seperator2 + ssStr);
}
/**
 * 获取今天时间数组
 * type 获取时间类型 1 字符串   2数组
 * 默认为1
 */
function getTodayDateArray() {
	var date = new Date();
	var seperator1 = '-';
	var seperator2 = ':';
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	var hhstr = date.getHours();
	var mmStr = date.getMinutes();
	var ssStr = date.getSeconds();
	if (month >= 1 && month <= 9) {
		month = '0' + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = '0' + strDate;
	}
	if (hhstr >= 0 && hhstr <= 9) {
		hhstr = '0' + hhstr;
	}
	if (mmStr >= 0 && mmStr <= 9) {
		mmStr = '0' + mmStr;
	}
	if (ssStr >= 0 && ssStr <= 9) {
		ssStr = '0' + ssStr;
	}

	return [year, month, strDate, hhstr, mmStr, ssStr];

}

/**
 * 获取月份天数
 * @param {Object} month
 */
function getDateDays(month) {
	var date = new Date();
	var year = date.getFullYear();
	if (month == 0) {
		month = date.getMonth() + 1;
	}
	var d = new Date(year, month, 0);
	return d.getDate();
}
/**
 * 获取当前月份
 */
function getDateMonth() {
	var date = new Date();
	var month = date.getMonth() + 1;
	return month;
}

export default {
	getNowFormatDate,
	getTodayDate,
	getDateDays,
	getDateMonth,
	getTodayDateArray,
	dateFormat,
	getSevenAfterDate
};
